package com.donn.securitycontrolplatform.common.service;

import com.dahuatech.icc.exception.ClientException;
import com.donn.securitycontrolplatform.alarmdetail.AlarmDetail;
import com.donn.securitycontrolplatform.alarmdetail.service.IAlarmDetailService;
import com.donn.securitycontrolplatform.common.Result;
import com.donn.securitycontrolplatform.hisalarm.entity.THisAlarm;
import com.donn.securitycontrolplatform.hisalarm.service.ITHisAlarmService;
import com.donn.securitycontrolplatform.rpmClient.TcpClient;
import com.donn.securitycontrolplatform.utils.DaHuaUtil;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * 文件描述
 *
 * @program: SecurityControlPlatform
 * @author: zhenglx
 * @create: 2022/6/13
 */
@Service
@Slf4j
public class CommonService {
    @Autowired
    ITHisAlarmService hisAlarmService;
    @Autowired
    DaHuaUtil daHuaUtil;
    @Autowired
    IAlarmDetailService alarmDetailService;


    @Value("${file.path}")
    private String FILE_PATH;

    @Autowired
    TcpClient client;

    private Object userinfoMapper;

    /**
     * 通用上传接口
     *
     * @param multipartFile file
     * @return 返回保存路劲
     */
    public Result upload(MultipartFile multipartFile) {
        if (multipartFile == null) {
            return new Result(500, "error ", "received file is null!");
        }
        log.info("文件content-type={}", multipartFile.getContentType());
        log.info("文件大小={}", multipartFile.getSize());
        log.info("文件名={}", multipartFile.getName());
        log.info("文件原始名={}", multipartFile.getOriginalFilename());
        // 保存接收的文件到本地
        File directory = new File(FILE_PATH);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        String filePath = directory.getAbsolutePath() + File.separator + multipartFile.getOriginalFilename();
        File destFile = new File(filePath);
        try {
            multipartFile.transferTo(destFile);
        } catch (IOException e) {
            log.error("An error occurred in the transferTo : ", e);
            return new Result(500, "error ", e.getMessage());
        }
        return Result.success(filePath.replaceAll("\\\\", "/"));
    }

    /**
     * 大华人脸布控消息订阅
     *
     * @param request
     * @return
     */
    public void receiveFaceMsg(String request) throws ClientException {
        JsonElement jsonElement = JsonParser.parseString(request);
        String alarm = jsonElement.getAsJsonObject().get("category").getAsString();
        if ("alarm".equals(alarm)) {
            String alarmDate = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("alarmDate").getAsString();
            int grade = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("alarmGrade").getAsInt();
            long alarmType = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("alarmType").getAsLong();
//            String uri = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("extend").getAsJsonObject().get("faceImageUrl").getAsString();
            String jkName = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("extend").getAsJsonObject().get("channelName").getAsString();
            String deviceCode = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("deviceCode").getAsString();

            String asString = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("orgName").getAsString();
            String imgUrl = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("extend").getAsJsonObject().get("globalScenePicUrl").getAsString();

            THisAlarm hisAlarm = new THisAlarm();
            hisAlarm.setAlarmType(alarmType);
            hisAlarm.setEquipmentNum(deviceCode);
            LocalDateTime time2 = LocalDateTime.ofEpochSecond(Long.parseLong(alarmDate), 0, ZoneOffset.ofHours(8));
            hisAlarm.setOperateTime(time2);
            hisAlarm.setDeviceName(jkName);
            hisAlarm.setImgUrl(imgUrl);
            hisAlarmService.save(hisAlarm);
            checkAlarmType(alarmType, grade, asString);
        }

        log.info("订阅的消息=" + request);
    }

    public void receiveMjMsg(String body) {
        JsonElement jsonElement = JsonParser.parseString(body);
        String deviceCode = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("deviceCode").getAsString();
        Long alarmType = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("alarmType").getAsLong();
        String imgUrl = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("alarmPicture").getAsString();
        Long date = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("alarmDate").getAsLong();
        String name = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("extend").getAsJsonObject().get("openTypeStr").getAsString();
        String deviceName = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("extend").getAsJsonObject().get("deviceName").getAsString();
        Integer grade = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("alarmGrade").getAsInt();
        String openTypeStr = jsonElement.getAsJsonObject().get("info").getAsJsonObject().get("extend").getAsJsonObject().get("openTypeStr").getAsString();

        THisAlarm hisAlarm = new THisAlarm();
        hisAlarm.setImgUrl(imgUrl);
        hisAlarm.setAlarmType(alarmType);
        hisAlarm.setDeviceName(deviceName);
        hisAlarm.setOperateTime(LocalDateTime.ofEpochSecond(date, 0, ZoneOffset.ofHours(8)));
        hisAlarm.setEquipmentNum(deviceCode);
        hisAlarmService.save(hisAlarm);

        checkAlarmType(alarmType, grade, openTypeStr);


        log.info("门禁消息来啦======" + body);


    }

    private void checkAlarmType(Long alarmType, Integer grade, String openTypeStr) {
        AlarmDetail detail = alarmDetailService.getByAlarmType(alarmType);
        if (ObjectUtils.isEmpty(detail)) {
            AlarmDetail alarm = new AlarmDetail();
            alarm.setAlarmType(alarmType);
            alarm.setGrade(grade);
            alarm.setName(openTypeStr);
            alarmDetailService.save(alarm);
        }
    }
}
